/**
 * jims
 */
package com.jims.master.modules.orders.service;

import com.google.common.collect.Maps;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.drug.dao.DrugStorageDeptDao;
import com.jims.master.modules.orders.dao.DrugDispMidDao;
import com.jims.master.modules.orders.entity.DrugDispMid;
import com.jims.master.modules.orders.entity.Orders;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 摆药中间表Service
 *
 * @author 陈晓阳
 * @version 2017-04-07
 */
@Service
@Transactional(readOnly = true)
public class DrugDispMidService extends CrudService<DrugDispMidDao, DrugDispMid> {

    @Autowired
    private DrugStorageDeptDao drugStorageDeptDao;
    public DrugDispMid get(String id) {
        return super.get(id);
    }

    public List<DrugDispMid> findList(DrugDispMid drugDispMid) {
        return super.findList(drugDispMid);
    }

    public Page<DrugDispMid> findPage(Page<DrugDispMid> page, DrugDispMid drugDispMid) {
        return super.findPage(page, drugDispMid);
    }

    @Transactional(readOnly = false)
    public int save(DrugDispMid drugDispMid) {
        return super.save(drugDispMid);
    }

    @Transactional(readOnly = false)
    public int delete(DrugDispMid drugDispMid) {
        return super.delete(drugDispMid);
    }

    /**
     * 根据医嘱明细保存中间表数据
     *
     * @param dispOrderDetails
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> saveData(List<Orders> dispOrderDetails) {
        Map<String, Object> returnVal = Maps.newHashMap();
        List<Orders> dispOrders=new ArrayList<Orders>();
        Integer rows = 0;
        String storageName = drugStorageDeptDao.get(dispOrderDetails.get(0).getStorageId()).getStorageName();
        String dispNo = String.valueOf(System.currentTimeMillis()).toString().substring(0, 10);
        for (Orders orders : dispOrderDetails) {
            if (orders.getTimes() < 1) {
                continue;
            }
            DrugDispMid drugDispMid = new DrugDispMid();
            drugDispMid.setDispNo(dispNo);
            drugDispMid.setPatientId(orders.getPatientId());
            drugDispMid.setPerformedBy(orders.getStorageId());
            drugDispMid.setAmount(orders.getAmount() + "");
            drugDispMid.setClassOnMr(orders.getClassOnMr());
            drugDispMid.setClassOnReckoning(orders.getClassOnReckoning());
            drugDispMid.setCosts((orders.getAmount() * orders.getPrice()));
            drugDispMid.setFirmName(orders.getFirmName());
            drugDispMid.setItemClass(orders.getOrderClass());
            drugDispMid.setItemName(orders.getOrderText());
            drugDispMid.setItemPrice(orders.getPrice());
            drugDispMid.setItemSpec(orders.getDrugSpec());
            drugDispMid.setItemUnit(orders.getDrugUnits());
            drugDispMid.setItemCode(orders.getDrugCode());
            drugDispMid.setNewDept(orders.getNewDept());
            drugDispMid.setName(orders.getName());
            drugDispMid.setOrdersId(orders.getId());
            drugDispMid.setOrgId(orders.getOrgId());
            drugDispMid.setPerformedPer(UserUtils.getUser().getId());
            drugDispMid.setNewDoct(orders.getNewDoct());
            drugDispMid.setStorageName(storageName);
            drugDispMid.setSubjCode(orders.getSubjCode());
            drugDispMid.setPackageNum(orders.getPackageNum());
            drugDispMid.setVisitId(orders.getVisitId());
            drugDispMid.setWardCode(orders.getWardCode());
            drugDispMid.setCurDispDate(orders.getCurrentDispEndDate());
            dispOrders.add(orders);
            rows += save(drugDispMid);
        }
        returnVal.put("rows", rows);
        returnVal.put("dispNo", dispNo);
        returnVal.put("dispOrders", dispOrders);
        return returnVal;
    }
}